<?php include 'head.php'; ?>
    <title>分类管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
</head>

<body class="pear-container">
    <div class="layui-card">
        <div class="layui-card-header"></div>
        <div class="layui-card-body">
            <table id="sort" lay-filter="sort"></table>
        </div>
    </div>
    <script type="text/html" id="sort-toolbar">
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增分类
        </button>
    </script>

    <script type="text/html" id="sort-bar">
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
    </script>
</body>
<script src="/component/layui/layui.js"></script>

<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['layer', 'element', 'popup', 'table', 'loading'], function() {
        var $ = layui.jquery,
            layer = layui.layer,
            element = layui.element,
            popup = layui.popup,
            table = layui.table,
            loading = layui.loading;

        table.render({
            elem: '#sort',
            height: 500,
            url: './ajax.php?act=getsort',
            page: true,
            escape: true,
            toolbar: '#sort-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            cols: [
                [{
                        field: 'id',
                        title: 'ID',
                        width: 80,
                        sort: true,
                        fixed: 'left'
                    }, {
                        field: 'name',
                        title: '分类名',
                    }, {
                        field: 'begin',
                        title: '开始时间',
                    }, {
                        field: 'end',
                        title: '结束时间',
                    },
                    {
                        title: '操作',
                        toolbar: '#sort-bar',
                        align: 'left',
                        width: 150,
                        fixed: 'right'
                    }
                ]
            ]
        });


        table.on('tool(sort)', function(obj) {
            if (obj.event === 'remove') {
                layer.confirm('确定要删除该分类？', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    loading.Load(1, "请稍等");
                    $.ajax({
                        url: "ajax.php?act=delsort",
                        type: "POST",
                        timeout: "5000",
                        data: {
                            id: obj.data['id']
                        },
                        success: function(result) {
                            loading.loadRemove();
                            if (result.code == 200) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    obj.del();
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }
        });

        table.on('toolbar(sort)', function(obj) {
            if (obj.event === 'add') {
                layer.open({
                    type: 2,
                    title: '新增分类',
                    shadeClose: true,
                    shade: false,
                    maxmin: true,
                    area: ['500px', '500px'],
                    content: 'addsort.php'
                });
            } else if (obj.event === 'refresh') {
                table.reload('sort');
            }
        });

    });
</script>

</html>